package com.xzq.action;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.inject.Inject;

import org.springframework.stereotype.Controller;

import com.xzq.biz.AqiBiz;
import com.xzq.entity.Aqi;
import com.xzq.entity.District;
import com.xzq.entity.Page;

@Controller
public class AqiAction extends BaseAction {
	@Inject
	private AqiBiz biz;
	private List<District> list = new ArrayList<District>();
	private Aqi aqi;
	private Aqi newAqi;
	private Page page;
	private District dis;
	private String time;
	private String likename;
	private int type;
	private int num;
	
	
	public int getNum() {
		return num;
	}
	public void setNum(int num) {
		this.num = num;
	}
	public String getLikename() {
		return likename;
	}
	public void setLikename(String likename) {
		this.likename = likename;
	}
	public String getTime() {
		return time;
	}
	public void setTime(String time) {
		this.time = time;
	}
	public Aqi getNewAqi() {
		return newAqi;
	}
	public void setNewAqi(Aqi newAqi) {
		this.newAqi = newAqi;
	}
	public void setType(int type) {
		this.type = type;
	}
	public Integer getType() {
		return type;
	}
	public void setType(Integer type) {
		this.type = type;
	}
	public District getDis() {
		return dis;
	}
	public void setDis(District dis) {
		this.dis = dis;
	}
	public List<District> getList() {
		return list;
	}
	public void setList(List<District> list) {
		this.list = list;
	}
	public Aqi getAqi() {
		return aqi;
	}
	public void setAqi(Aqi aqi) {
		this.aqi = aqi;
	}
	public Page getPage() {
		return page;
	}
	public void setPage(Page page) {
		this.page = page;
	}
	
	public String toAddDis(){
		return "toAddDis";
	}
	
	public String toAddAqi(){
		list = biz.findAllDis();
		return "toAddAqi";
	}
	
	public String addDis(){
		Integer count = biz.saveDis(dis);
		if(count!=-1){
			return show();
		}else{
			return "fail";
		}
	}
	
	public String addAqi(){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date = null;
		try {
			date = sdf.parse(time);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		aqi.setMonitor_time(date);
		Integer count = biz.saveAqi(aqi);
		if(count!=-1){
			return "add_success";
		}else{
			return "fail";
		}
	}
	
	public String show(){
		list = biz.findAllDis();
		page = biz.findAllAqiByPage(page,type);
		return "show";
	}
	
	public String disShow(){
		num = 0;
		if(likename==null){
			likename = "";
		}
		if(!"".equals(likename)){
			num = 1;
		}
		page = biz.findAllDisByPage(page, likename);
		return "disShow";
	}
	
	public String toModifyAqi(){
		aqi = biz.findOneAqi(aqi);
		list = biz.findAllDis();
		return "toModfyAqi";
	}
	
	public String modifyAqi(){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date = null;
		try {
			date = sdf.parse(time);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		newAqi.setMonitor_time(date);
		Integer count = biz.modifyAqi(newAqi);
		if(count==1){
			return show();
		}else{
			return "fail";
		}
	}
	
	public String deleteAqi(){
		Integer count = biz.deleteAqi(aqi);
		if(count==1){
			return show();
		}else{
			return "fail";
		}
	}
	
	public String toModifyDis(){
		dis = biz.findOneDis(dis);
		return "toModifyDis";
	}
	
	public String modifyDis(){
		Integer count = biz.modifyDis(dis);
		if(count==1){
			return disShow();
		}else{
			return "fail";
		}
	}
	
	public String deleteDis(){
		Integer count = biz.deleteDis(dis);
		if(count==1){
			return disShow();
		}else{
			return "fail";
		}
	}
}
